package com.test.p06prototype.example01;

import java.io.*;
import java.util.Date;

/**
 * 序列化反序列化实现深复制
 */
public class Client03 {
    public static void main(String[] args) throws Exception {
        Sheep sheep = new Sheep();
        sheep.setBirthday(new Date());
        sheep.setName("duoli");

        System.out.println(sheep);


        //使用序列化反序列化实现深复制
        //序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(sheep);
        byte[] bytes = bos.toByteArray();

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        Sheep sheep2 =(Sheep) ois.readObject();
        sheep.getBirthday().setTime(113493423434L);
        System.out.println(sheep.getBirthday());
        System.out.println(sheep2.getBirthday());
    }
}
